<?php

namespace Home\Controller;

use Think\Controller;

class BeginController extends Controller {

    public function index() {
        if (!session('?member')) {
            $Subject = D('Subject');
            $map = array();
            $map["id"] = $_GET['id'];
            $list = $Subject->where($map)->find();
            $this->assign('list', $list);
            $this->display('begin');
        } else {
            $memberModel = M('Member');
            //判断该科目是否存在
            $map['sid'] = $_GET['id'];
            $map['id'] = session('member');
            $member = $memberModel->where($map)->find();
            if (!empty($member)) {
                session('sid', $map['sid']);
                $this->redirect('Questions/index?id=' . $_GET['id']);
            } else {
                $Subject = D('Subject');
                $map = array();
                $map["id"] = $_GET['id'];
                $list = $Subject->where($map)->find();
                $this->assign('list', $list);
                $this->display('begin');
            }
        }
    }

    public function jaxaMessage() {
        $memberModel = M('Member');
        $data['name'] = $_POST['username'];
        $data['phone'] = $_POST['phone'];
        $data['sid'] = $_POST['sid'];
        $member = $memberModel->where($data)->find();
        if (empty($member)) {
            $data['date'] = date('Y-m-d H:i:s');
            if (session('?member')) {
                $data['id'] =session('member');
                 $res = $memberModel->add($data);
            }else{
                $res = $memberModel->add($data);
                session('member', $res);
            }
            if ($res!==false) {
                session('sid', $_POST['sid']);
            }
        }
    }

}